package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.OrdersubmitDao;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.dto.VeiflcationcodeParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderSubmitService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class OrdersubmitServicrImpl implements OrderSubmitService {
    @Autowired
    OrdersubmitDao ordersubmitDao;
    @Autowired
    OrderSettingDao orderSettingDao;
    /*
    * 添加预约
    * */
    @Override
    public void addordersubmit(OrderSubmitParam orderSubmitParam) {
        //查询预约人是否一满
        OrderSettingParam orderSettingParam =  ordersubmitDao.selectnumber(orderSubmitParam.getOrderDate());
        //判断这一天是否设有预约
        if (orderSettingParam == null){
            throw new BusinessRuntimeException(MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断预约人数是否超过可预约人数
        if (orderSettingParam.getNumber() > orderSettingParam.getReservations()){
            //没增加一个用户就想预约设置表中添加一个预约人
            orderSettingDao.updatesubNumber(orderSubmitParam.getOrderDate());
        }else {
            throw new BusinessRuntimeException(MessageConst.ORDER_FULL);
        }

        //新增预约返回一个主键id
        orderSubmitParam.setRegTime(LocalDateTime.now());
        ordersubmitDao.addsubmit(orderSubmitParam);

        Integer orderid = orderSubmitParam.getId();
        //向预约详情表中插入数据
        Order order = new Order();
        //复制数据
        order.setMemberId(orderid);
        order.setOrderDate(orderSubmitParam.getOrderDate());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(orderSubmitParam.getSetMealId());
        //向数据库中插入数据
        ordersubmitDao.addOrder(order);



    }
/*
* 查询验证码是否一样
* */
    @Override
    public VeiflcationcodeParam selectCode(String telephone) {
        //根据手机号查询验证码
       VeiflcationcodeParam v =  ordersubmitDao.selectCode(telephone);
        return v;
    }
/*
* 查询是否重复预约
* */
    @Override
    public Integer selectrepeat(Date orderDate, String idCard) {
      Integer count=  ordersubmitDao.selectrepeat(orderDate,idCard);
        return count;
    }
}
